# -*- coding:utf-8 -*-


import os
import datetime
import re
import chinese_calendar

"""
中国节假日
pip install chinesecalendar

https://pypi.org/project/chinesecalendar/
"""



def ConvertWenZi():
    date_pattern1 = r'\d{4}-\d{2}-\d{2}'
    date_pattern2 = r'\d{4}年\d{1,2}月\d{1,2}日'

    with open('logs.txt', 'r', encoding='utf-8') as file:
        line = file.readline()
        strTarget = ''
        currentDate = ''  # 将相同日期中的内容整理在一起
        nextDate = ''
        while line:
            line = line.strip()
            matches1 = re.findall(date_pattern1, line)
            matches2 = re.findall(date_pattern2, line)
            if line.__len__() > 1:
                # 如果是日期
                if matches1.__len__() > 0 or matches2.__len__() > 0:
                    if matches1.__len__() > 0:
                        riqi = line.replace('\n', '')
                        date1 = datetime.datetime.strptime(riqi, "%Y-%m-%d")
                        line = date1.strftime("%Y年%m月%d日")
                    currentDate = line
                    if currentDate != nextDate:
                        nextDate = currentDate
                        strTarget = line
                    print(line)
                # 去掉行开头的“冒号”
                elif line.split(':').__len__() > 0:
                    line = "".join(line.split(':'))
                    strTarget += line

                if strTarget.__len__() > 0:
                    with open("11111.txt", "a", encoding="utf-8") as fp:
                        fp.write(strTarget + '\n')
                    strTarget = ""


            line = file.readline()

def convert_RQ():
    with open('logs.txt', 'r', encoding='utf-8') as file:
        line = file.readline()
        while line:
            line = line.strip()
            matches = re.findall(r'\d{4}年\d{1,2}月\d{1,2}日', line)
            if matches.__len__() > 0:
                line = line.strip()
                line = line.replace('年', '-')
                line = line.replace('月', '-')
                line = line.replace('日', '')
                list_rq = line.split('-') # 格式为yyyy-MM-dd
                # 判断工作日
                april_last = datetime.date(int(list_rq[0]), int(list_rq[1]), int(list_rq[2]))
                flag = chinese_calendar.is_workday(april_last)
                print('第一次判断工作日:' + line)

                while flag == False:
                    april_last = april_last + datetime.timedelta(days=1)
                    flag = chinese_calendar.is_workday(april_last)
                    print('判断工作日:' + line)

               # 判断法定节假日
                flag = chinese_calendar.is_holiday(april_last)
                print('第一次判断法定节假日:' + line)

                while flag == True:
                    april_last = april_last + datetime.timedelta(days=1)
                    flag = chinese_calendar.is_holiday(april_last)
                    print('判断法定节假日:' + line)

                line = april_last.strftime("%Y年%m月%d日")

            with open("11111.txt", "a", encoding="utf-8") as fp:
                fp.write(line + '\n')

            line = file.readline()





if __name__ == '__main__':
    # ConvertWenZi()  # 转文字

    convert_RQ()
    #
    april_last = datetime.date(2018, 4, 30)
    print(chinese_calendar.is_workday(april_last))  # 判断是不是工作日
    print(april_last.strftime("%Y-%m-%d"))
    # chinese_calendar.is_holiday(april_last)  # 判断是不是节假日
    # holiday_name = chinese_calendar.Holiday.labour_day.value  # 获取节日名称
    # chinese_calendar.is_in_lieu(datetime.date(2006, 2, 1))  # 判断法定节假日是不是调休
    # chinese_calendar.is_in_lieu(datetime.date(2006, 2, 2))  # 判断法定节假日是不是调休
